using System.Collections;
using UnityEngine;

public class DailyCoinsInterface : MonoBehaviour
{
	private const float TIME_TO_CANCEL = 10f;

	public GameObject watchVideoPanel;

	public GameObject dailyCoinsUnavailablePanel;

	public GameObject videoUnavailablePanel;

	public GameObject rewardMessage;

	public GameObject backGroundCollider;

	public GameObject iconGold;

	public GameObject iconCash;

	public GameObject iconCashReward;

	public GameObject iconGoldReward;

	public GameObject blockPannel;

	public GameObject exit;

	public UILabel rewardLabel;

	public UILabel lbRewardGetBonuse;

	private int _availableReward;

	private string _availableRewardType;

	private string _currentRewardType;

	private int _currentReward;

	private bool _videoAvailable;

	private IEnumerator _waitVideoAvailable;

	public void Init()
	{
		FyberWrapper.successVideoWatch -= SuccessVideoWatchEventHandler;
		FyberWrapper.canceledVideoWatch -= CanceledVideoWatchEventHandler;
		FyberWrapper.dailyCoinsUnavailable -= DailyCoinsUnavailableEventHandler;
		FyberWrapper.nextVideo -= NextVideoEventHandler;
		FyberWrapper.videoAvailability -= VideoAvailabilityEventHandler;
		FyberWrapper.successVideoWatch += SuccessVideoWatchEventHandler;
		FyberWrapper.canceledVideoWatch += CanceledVideoWatchEventHandler;
		FyberWrapper.dailyCoinsUnavailable += DailyCoinsUnavailableEventHandler;
		FyberWrapper.nextVideo += NextVideoEventHandler;
		FyberWrapper.videoAvailability += VideoAvailabilityEventHandler;
		if (FyberWrapper.instance != null)
		{
			FyberWrapper.instance.CheckRVAvailability();
			_videoAvailable = FyberWrapper.instance.rewardedVideoAvailable;
		}
	}

	public void Unsubscribe()
	{
		FyberWrapper.successVideoWatch -= SuccessVideoWatchEventHandler;
		FyberWrapper.canceledVideoWatch -= CanceledVideoWatchEventHandler;
		FyberWrapper.dailyCoinsUnavailable -= DailyCoinsUnavailableEventHandler;
		FyberWrapper.nextVideo -= NextVideoEventHandler;
		FyberWrapper.videoAvailability -= VideoAvailabilityEventHandler;
	}

	private void NextVideoEventHandler(int reward, string rewardType)
	{
		BCWDebug.Log("_availableReward: " + reward + " " + rewardType, "Fyber");
		_availableReward = reward;
		_availableRewardType = rewardType;
		if (_availableRewardType.Equals("gold"))
		{
			iconGold.SetActive(true);
			iconCash.SetActive(false);
		}
		else
		{
			iconGold.SetActive(false);
			iconCash.SetActive(true);
		}
		rewardLabel.text = _availableReward.ToString();
		if (!rewardMessage.activeSelf)
		{
			exit.SetActive(true);
			watchVideoPanel.gameObject.SetActive(true);
		}
		dailyCoinsUnavailablePanel.gameObject.SetActive(false);
		videoUnavailablePanel.gameObject.SetActive(false);
	}

	private void VideoAvailabilityEventHandler(bool availability)
	{
		BCWDebug.Log("VideoAvailabilityEventHandler: " + availability);
		_videoAvailable = availability;
		if (!_videoAvailable)
		{
			watchVideoPanel.gameObject.SetActive(false);
			dailyCoinsUnavailablePanel.gameObject.SetActive(false);
			exit.SetActive(true);
			videoUnavailablePanel.gameObject.SetActive(true);
			if (blockPannel != null)
			{
				blockPannel.SetActive(false);
			}
		}
	}

	private void SuccessVideoWatchEventHandler()
	{
		exit.SetActive(false);
		if (_availableRewardType.Equals("gold"))
		{
			iconGoldReward.SetActive(true);
			iconCashReward.SetActive(false);
		}
		else
		{
			iconGoldReward.SetActive(false);
			iconCashReward.SetActive(true);
		}
		lbRewardGetBonuse.text = _availableReward.ToString();
		rewardMessage.SetActive(true);
		watchVideoPanel.SetActive(false);
	}

	private void CanceledVideoWatchEventHandler()
	{
		backGroundCollider.gameObject.SetActive(false);
	}

	private void DailyCoinsUnavailableEventHandler()
	{
		if (base.gameObject.activeSelf)
		{
			StartCoroutine(WaitCloseGetReward());
		}
	}

	private IEnumerator WaitCloseGetReward()
	{
		while (rewardMessage.activeSelf)
		{
			yield return null;
		}
		watchVideoPanel.gameObject.SetActive(false);
		exit.SetActive(true);
		dailyCoinsUnavailablePanel.gameObject.SetActive(true);
		videoUnavailablePanel.gameObject.SetActive(false);
	}

	public void GetReward()
	{
		backGroundCollider.gameObject.SetActive(false);
		rewardMessage.gameObject.SetActive(false);
		if (_currentRewardType.Equals("gold"))
		{
			settings.ShowLabelWithAnimation(_currentReward, Vector3.zero, false);
		}
		else
		{
			settings.ShowSpecialLabelWithAnimation(_currentReward, Vector3.zero, false);
		}
		settings.SaveCathedExeptionsToStorager();
		rewardMessage.gameObject.SetActive(false);
		exit.SetActive(true);
		watchVideoPanel.SetActive(true);
		_currentReward = 0;
	}

	public void ShowVideo()
	{
		if (blockPannel == null)
		{
			if (controllerMenu.thisScript != null)
			{
				blockPannel = controllerMenu.thisScript.blockPanel.gameObject;
			}
			else if (GameController.instance != null)
			{
				blockPannel = GameController.instance.blockPanel.gameObject;
			}
		}
		_currentReward = _availableReward;
		_currentRewardType = _availableRewardType;
		if (_waitVideoAvailable == null)
		{
			_waitVideoAvailable = WaitVideoAvailable();
			StartCoroutine(_waitVideoAvailable);
		}
	}

	private IEnumerator WaitVideoAvailable()
	{
		BCWDebug.LogError("Start Wait Video", "Fyber");
		backGroundCollider.gameObject.SetActive(true);
		if (!_videoAvailable)
		{
			BCWDebug.LogError("video not avaible", "Fyber");
			blockPannel.SetActive(true);
			float start = Time.realtimeSinceStartup;
			FyberWrapper.instance.CheckRewardedVideo(RVRequestLocation.Button);
			while (!_videoAvailable)
			{
				if (Time.realtimeSinceStartup < start + 10f)
				{
					yield return new WaitForEndOfFrame();
					continue;
				}
				_waitVideoAvailable = null;
				blockPannel.SetActive(false);
				backGroundCollider.gameObject.SetActive(false);
				yield break;
			}
		}
		BCWDebug.LogError("Start Show Video", "Fyber");
		blockPannel.SetActive(false);
		if (FyberWrapper.instance != null)
		{
			FyberWrapper.instance.ShowRewardedVideo();
		}
		_videoAvailable = false;
		_waitVideoAvailable = null;
	}

	public void HideWindow()
	{
		base.gameObject.SetActive(false);
	}
}
